﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KQ.DCMMP.Common
{
    public static class StringUtility
    {
        /// <summary>
        /// 转换为全角
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string ToSBC(String input)
        {
            // 半角转全角
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 32)
                {
                    c[i] = (char)12288;
                    continue;
                }
                if (c[i] < 127)
                {
                    c[i] = (char)(c[i] + 65248);
                }
            }
            return new String(c);
        }

        /// <summary>
        /// 全角转半角
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string ToDBC(String input)
        {
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 12288)
                {
                    c[i] = (char)32;
                    continue;
                }
                if (c[i] > 65280 && c[i] < 65375)
                {
                    c[i] = (char)(c[i] - 65248);
                }
            }

            return new String(c);
        }

        /// <summary>
        /// 中文转化成阿拉伯数字
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string ToArab(String input)
        {
            List<string> chinese = new List<string>();
            chinese.Add("一");
            chinese.Add("二");
            chinese.Add("三");
            chinese.Add("四");
            chinese.Add("五");
            chinese.Add("六");
            chinese.Add("七");
            chinese.Add("八");
            chinese.Add("九");
            chinese.Add("十");

            Dictionary<int, string> chineseDict = new Dictionary<int, string>();

            StringBuilder resultBuilder = new StringBuilder();
            StringBuilder chineseBuilder = new StringBuilder();

            bool isNumber = false;

            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                foreach (string item in chinese)
                {
                    if (string.Equals(c[i].ToString(), item))
                    {
                        isNumber = true;
                        chineseDict.Add(i, c[i].ToString());
                        chineseBuilder.Append(c[i].ToString());
                        break;
                    }
                }

                if (!isNumber)
                {
                    //首先把ChineseBuilder中的文字进行转换
                    if (chineseBuilder.Length > 0)
                    {
                        resultBuilder.Append(new NumberConventer().ChnToArab(chineseBuilder.ToString()));
                        chineseBuilder = new StringBuilder();
                    }
                    resultBuilder.Append(c[i].ToString());
                }

                isNumber = false;
            }

            return resultBuilder.ToString();
        }
    }
}
